package wyklad;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TextToHtmlFilter implements Filter {
	public void init(FilterConfig fc) throws ServletException {}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		
		ZawszeOdpowiadajWrapper zowr = new ZawszeOdpowiadajWrapper(req);
		ZamienNaHtmlWrapper znwr = new ZamienNaHtmlWrapper(res);
		
		filterChain.doFilter(zowr, znwr);
		
		String prefix = "<html><body background=\"tlo.png\"><pre>";
		String suffix = "</pre></body></html>";
		
		StringBuilder sb = new StringBuilder(prefix);
		String oryginalnyDokument = new String(znwr.toByteArray());
		sb.append(oryginalnyDokument);
		sb.append(suffix);
		
		res.setContentType("text/html;charset=utf8");
		res.setContentLength(sb.length());
		
		PrintWriter pw = res.getWriter();
		pw.println(sb.toString());
	}

	public void destroy() {}
}
